package com.gitee.huxing;

import com.gitee.huxing.course.netty.gateway.listener.NettyGateWayListener;
import com.gitee.huxing.course.netty.listener.NettyListener;
import com.gitee.huxing.course.netty.server.listener.NettyServerListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * 描述: Netty服务路由网关系统启动
 * @Author: huxing
 * @packg: com.gitee.huxing.week03
 * Created by Interllij IDEA.
 * DateTime: 2019-03-26 下午3:55
 */

@Slf4j
@SpringBootApplication
public class NettyServerApplication implements CommandLineRunner {

    static {
        try {
            System.setProperty("log4fFile", "week03");
        } catch (Exception ex){
            ex.printStackTrace();
        }
    }

    /** 线程池核心大小数 **/
    private static final int corePoolSize = 3;

    /** 最大线程数 **/
    private static final  int maximumPoolSize = corePoolSize * 10;

    /** 超过corePoolSize的线程多久不活动被销毁时间 **/
    private static final long keepAliveTime = 30L ;

    /** 时间单位 **/
    private static final TimeUnit unit = TimeUnit.SECONDS;

    /** 任务队列 **/
    private static BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();

    /** 线程工厂 **/
    private ThreadFactory threadFactory;

    /** 拒绝策略 **/
    private RejectedExecutionHandler handler;

    public static void main(String[] args) {
        SpringApplication.run(NettyServerApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize
                ,keepAliveTime, unit, workQueue);
        // 后台服务节点1
        NettyServerListener listener1 = new NettyServerListener(8088, "Hi Welcome to 8088/Netty Server!");
        // 后台服务节点2
        NettyServerListener listener2 = new NettyServerListener(8089, "Hi Welcome to 8089/Netty Server!");
        // 网关服务
        NettyGateWayListener gateWayListener = new NettyGateWayListener(9098, null);
        // 通过线程池启动服务
        try {
            List<Runnable> tasks = new ArrayList<>();
            tasks.add(new NettyRunnable(listener1));
            tasks.add(new NettyRunnable(listener2));
            tasks.add(new NettyRunnable(gateWayListener));
            tasks.forEach(executor::execute);
        } catch (Exception ex){
            ex.printStackTrace();
            log.info("NettyHttp服务(后台)启动失败: {}", ex.getMessage());
        }
    }

    static class NettyRunnable implements Runnable{

        /** netty 服务 **/
        private final NettyListener listener;

        public NettyRunnable(NettyListener listener){
            this.listener = listener;
        }

        @Override
        public void run() {
            try {
                listener.run();
            } catch (Exception ex){
                log.info("NettyHttp服务(后台)启动失败: {}", ex.getMessage());
            }
        }
    }
}
